﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Huanan.Algorithm.DSP;
using Newtonsoft.Json;
using WpfApp1.Model;

namespace WpfApp1
{
    /// <summary>
    /// HRVCAL.xaml 的交互逻辑
    /// </summary>
    public partial class HRVCAL : Window
    {
        public HRVCAL()
        {
            InitializeComponent();
        }
        private HRVSTRUCT hRVSTRUCT = new HRVSTRUCT();
        string path = @"D://4cb6440f-158d-4c5a-88fc-66c7bd483e20.vcg.json";

        public  class HRVINFO
        {
            public int ID;
            public int index { set; get; }  //时间位置 R 波位置
           

            public int heartrate { set; get; }  //心率值

            public int RR { set; get; }



        }
        private allHbiv hr_;

        public allHbiv CurHr
        {
            get
            {
                return hr_;
            }
            set
            {
                hr_ = value;
                if (this.PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("CurHr"));
                }
            }

        }

 

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null == handler)
            {
                return;
            }
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
        private int _heartbeat=0;

        public int  Heartrate
        {
            get
            {
                return _heartbeat;
            }
            set
            {
                _heartbeat = value;
                if (this.PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Heartrate"));
                }
            }

        }


        private List<HRVINFO> hrates=new List<HRVINFO>();

        private void Display()
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {

                //var item = hRVSTRUCT.allhbiv[cbxecgindex.SelectedIndex];

            }));

        }

        List<int> listRR = new List<int>();
            
        private void Init()
        {

            if (!File.Exists(path))
            {

                System.Windows.  MessageBox.Show(path+"不存在");
                return;
            }
            hRVSTRUCT = JsonConvert.DeserializeObject<HRVSTRUCT>(File.ReadAllText(path));
            hrates.Clear();
            listRR.Clear();
            //计算心率值 

            for (int i =0; i < hRVSTRUCT.allhbiv.Count-1; i++)
            {
                HRVINFO hi = new HRVINFO();
                hi.ID = i;
                hi.index = hRVSTRUCT.allhbiv[i].QRSTop;

                hi.RR = hRVSTRUCT.allhbiv[i+1].QRSTop - hRVSTRUCT.allhbiv[i].QRSTop;
                hi.heartrate = (int)(60000/hi.RR);

                hrates.Add(hi);

            }

            //统计心率 直方图 数据 


            //计算 R-R间期  
            int sum = 0;
            for (int i=0;i< hrates.Count;i++)
            {
                sum += hrates[i].RR ;
            
            }
            //平均值
            int averagerr = (int)(sum / hrates.Count);
            //
            int SD = 0;
            for (int i = 0; i < hrates.Count; i++)
            {
                SD += (hrates[i].RR - averagerr) * (hrates[i].RR - averagerr);

            }
            double   SD1 = Math.Sqrt(SD / hrates.Count);

            //计算 RSMMD
            int rsmmd = 0;

            int PNNCOunt = 0;
            List<int> listsdsd = new List<int>(); //计算差值
            for (int i = 0; i < hrates.Count-1; i++)
            {
                if (Math.Abs(hrates[i + 1].RR - hrates[i].RR)>50)
                {

                    PNNCOunt++;
                }
                listsdsd.Add(hrates[i + 1].RR - hrates[i].RR);
                //rsmmd += (hrates[i + 1].RR - hrates[i].RR) * (hrates[i + 1].RR - hrates[i].RR);
            }

            for (int i = 0; i < listsdsd.Count; i++)
            {
                rsmmd +=  listsdsd[i]*listsdsd[i];
            }
           double rsmmd1 =Math.Sqrt(rsmmd / listsdsd.Count);

            int sdsd = 0;

            int sumsdsd = 0;
            for (int i=0;i< listsdsd.Count;i++)
            {
                sumsdsd += listsdsd[i];
            }

            int aeragesd = sumsdsd / listsdsd.Count;

            for (int i=0;i<listsdsd.Count;i++)
            {
                sdsd += (listsdsd[i] - aeragesd) * (listsdsd[i] - aeragesd);
            }
            double sdsd1=  Math.Sqrt(sdsd / listsdsd.Count);

            PNNCOunt = PNNCOunt * 100 / listsdsd.Count;

            //SDDindex 

            //计算时间
     
            Dispatcher.BeginInvoke(new Action(() =>
            {
                tbxecgCount.Text = hRVSTRUCT.allhbiv.Count.ToString();
                for (int i = 1; i < hRVSTRUCT.allhbiv.Count; i++)
                {
                    cbxecgindex.Items.Add(i);
                }
                cbxecgindex.SelectedIndex = 1;
                hr_ = hRVSTRUCT.allhbiv[cbxecgindex.SelectedIndex];
                wraphr.DataContext = hr_;

                tbxSDNN.Text = SD1.ToString("F2");
                tbxRMSSD.Text = rsmmd1.ToString("F2");

                tbxSDSD.Text = sdsd1.ToString("F2");

                tbxAVNN.Text = averagerr.ToString("F2");

                tbxPNN50.Text = PNNCOunt.ToString("F2") +"%";

                tbxHRMax.Text   = hrates.Max(s=>s.heartrate).ToString();

                tbxHRMin.Text = hrates.Min(s => s.heartrate).ToString();

            }));

            //RR 间期
            listRR = hrates.Select(s=>s.RR).ToList();

            Draw();

        }
        DispatcherTimer timer = new DispatcherTimer();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Init();

            btmlead. SelectionChanged += btmlead_SelectionChanged;

            btmSpedd.SelectionChanged += btmlead_SelectionChanged;

            btmApmlity.SelectionChanged += btmlead_SelectionChanged;

            slider.ValueChanged += slider_ValueChanged;

            //ReadData(@"D://4cb6440f-158d-4c5a-88fc-66c7bd483e20.secg");
            // DrawECG();

            // txtCalHr.DataContext = this;
            timer.Tick += Timer_Tick;
            timer.Interval = TimeSpan.FromMilliseconds(100);
           

        }

      

        private void cbxecgindex_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {
                hr_ = hRVSTRUCT.allhbiv[cbxecgindex.SelectedIndex];
                wraphr.DataContext = hr_;
            }));

        }
        int leadCount = 17;
        double[][] ecgData = null;
        int singleDataLength = 0;
        public void ReadData(string datapath)
        {
            if (!File.Exists(datapath))
            {
                return;
            }
        
            FileStream  fs = new FileStream(datapath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader binReader = new BinaryReader(fs);
            byte[] verifyArray = binReader.ReadBytes((int)fs.Length);
            long dataLength = fs.Length / sizeof(short);
            double[] ecgSourceData;
            ecgSourceData = new double[dataLength];
            short[] ecgSourceDataShort = new short[dataLength];
            Buffer.BlockCopy(verifyArray, 0, ecgSourceDataShort, 0, verifyArray.Length);
            for (int i = 0; i < dataLength; i++)
            {
                ecgSourceData[i] = (double)ecgSourceDataShort[i];
            }
            fs.Close();
            binReader.Close();
           
            singleDataLength= Convert.ToInt32(dataLength / leadCount);

            ecgData= new double[leadCount][];
            
            for (int i = 0; i < leadCount; i++)
            {
                ecgData[i] = new double[singleDataLength];
                
            }

            for (int i = 0; i < leadCount; i++)
            {
                for (int j = 0; j < singleDataLength ; j++)
                {
                
                    ecgData[i][j] = ecgSourceData[j * leadCount + i];
                }
            }

        }
        private WriteableBitmap wBitmap;
        private WriteableBitmap wBitmapecg;
        //绘制 曲线图
        private void Draw()
        {

            int width = (int)outCanvas.ActualWidth;
            int height = (int)outCanvas.ActualHeight;
            if (width > 0 && height > 0)
            {
                DisplayIamgeHR.Width = width;
                DisplayIamgeHR.Height = height;
                wBitmap = new WriteableBitmap(width, height, 72, 72, PixelFormats.Bgr24, null);
                DisplayIamgeHR.Source = wBitmap;
            }
            wBitmap.Lock();
            Bitmap backBitmap = new Bitmap(width,height,wBitmap.BackBufferStride,System.Drawing.Imaging.PixelFormat.Format24bppRgb,wBitmap.BackBuffer);
            Graphics graphics = Graphics.FromImage(backBitmap);
            graphics.Clear(System.Drawing.Color.Gray);
            if (Dstyle==0)
            {
                for (int i = 0; i < hrates.Count - 1; i++)
                {
                    graphics.DrawLine(Pens.Red, i * 2, 200 - (hrates[i].heartrate - 60) * 4, (i + 1) * 2, 200 - (hrates[i + 1].heartrate - 60) * 4);
                }
                Font drawFont = new Font("Arial", 8);
                SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Yellow);

                graphics.DrawString("心率趋势图:", drawFont, drawBrush, 5, 10);
                if (curentindex != -1 && curentindex < hrates.Count * 2)
                {
                    //绘制定位线
                    graphics.DrawLine(Pens.Yellow, curentindex, 0, curentindex, height);
                    graphics.DrawString("当前心率:" + hrates[curentindex / 2].heartrate.ToString(), drawFont, drawBrush, curentindex - 10, height / 2);


                }
            }
            if (Dstyle == 1)
            {
                Font drawFont = new Font("Arial", 8);
                SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Yellow);
                graphics.DrawString("心率直方图:", drawFont, drawBrush, 5, 10);

                //
                Dictionary<int, int> dict = new Dictionary<int, int>();
                if (hrates.Count>0)
                {
                    dict = hrates.GroupBy(a => a.heartrate).Where(g => g.Count() > 1).ToDictionary(x=>x.Key,y=>y.Count());
                }
                System.Drawing.  Pen p = new System.Drawing.Pen(System.Drawing. Brushes.Yellow,1);
                graphics.DrawLine(p, 20, 20, 20, height);

                //绘制坐标
                graphics.DrawLine(Pens.Red,20, 200, width-20, 200 );
              
                int xdis= 0;
                Dictionary<int, int> dict1 = new Dictionary<int, int>();
                dict1 = dict.OrderBy(o => o.Key).ToDictionary(o=>o.Key,yp=>yp.Value);  //排序
                foreach (var item in dict1)
                {
                    System.Drawing.Pen p1 = new System.Drawing.Pen(System.Drawing.Brushes.Yellow, 2);
                    xdis++;
 
                    graphics.DrawLine(p1, 100 + xdis * 30, 200,100 + xdis * 30, 200 - item.Value);
                   
                    SolidBrush drawBrush1 = new SolidBrush(System.Drawing.Color.Black);
                    graphics.DrawString(item.Key.ToString(), drawFont , drawBrush1, 100 + xdis * 30-5, 200);

                }

            }
            if (Dstyle == 2)
            {
                Font drawFont = new Font("Arial", 8);
                SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Yellow);
                graphics.DrawString("频谱图:", drawFont, drawBrush, 5, 10);

                
                System.Drawing.Pen p = new System.Drawing.Pen(System.Drawing.Brushes.Yellow, 1);
                graphics.DrawLine(p, 20, 20, 20, height);

                int xdis = (width - 20) / calHeart.m_vecFreqRR.Count;
                //绘制坐标
                graphics.DrawLine(Pens.Red, 20, 200, width - 20, 200);

                for (int i=0;i< calHeart.m_vecFreqRR.Count-1;i++)
                {
                    System.Drawing.Pen p1 = new System.Drawing.Pen(System.Drawing.Brushes.Yellow, 2);
                     
                    graphics.DrawLine(p1, 20 + xdis*i , 200 - (int)calHeart.m_vecPSDRR[i] / 100, 20 + xdis * (i+1), 200 - (int)calHeart.m_vecPSDRR[i+1]/100);

                }
                double[] xzb = new double[6] { 0.0,0.1,0.2,0.3,0.4,0.5};

                for (int i=0;i<6;i++)
                {
                    SolidBrush drawBrush1 = new SolidBrush(System.Drawing.Color.Black);
                    graphics.DrawString(xzb[i].ToString(), drawFont, drawBrush1, 20 +i *(width-20)/6 - 5, 200);
                }
    
            }


            if (Dstyle == 3)
            {
                Font drawFont = new Font("Arial", 8);
                SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Yellow);
                graphics.DrawString("RR间期-散点图:", drawFont, drawBrush, 5, 10);

                System.Drawing.Pen p = new System.Drawing.Pen(System.Drawing.Brushes.Yellow, 1);

                //坐标点 （20，620） 500个像素点 代表1000
                graphics.DrawLine(p, 20, 620, 20, 620-500);

                
                for (int i=0;i<11;i++)
                {
                    graphics.DrawLine(p, 20+50*i, 620, 20 + 50 * i, 620 - 500);
                    graphics.DrawString(  (100*i).ToString(), drawFont, drawBrush, 10 + 50 * i, 620);
                }
                graphics.DrawString( "RR(n)/ms:", drawFont, drawBrush,50+500 , 620  );
                graphics.DrawLine(p, 20, 620, 20+500,620);

                for (int i = 0; i < 11; i++)
                {
                    graphics.DrawLine(p, 20 , 620-50*i, 20 +520, 620 - 50 * i);

                    graphics.DrawString((100 * i).ToString(), drawFont, drawBrush, 1 , 610- 50 * i);
                }

                graphics.DrawString("RR(n+1)/ms:", drawFont, drawBrush, 20, 100);

                SolidBrush drawBrushdot = new SolidBrush(System.Drawing.Color.Red);
                System.Drawing.Pen pdot = new System.Drawing.Pen(System.Drawing.Brushes.Red, 1);
                for (int i = 0; i < listRR.Count - 1; i++)
                {
                    int x = 20 + listRR[i] / 2;
                    int y = 620-  listRR[i + 1] / 2;
                    graphics.DrawRectangle(pdot, x, y, 1, 1);
                }

                graphics.DrawString("RR差值-散点图:", drawFont, drawBrush,700, 10);


                for (int i =1; i < listRR.Count - 1; i++)
                {
                    int x =820 +( listRR[i]- listRR[i-1]);
                    int y = 320 - (listRR[i + 1]-listRR[i]);
                    graphics.DrawRectangle(pdot, x, y, 1, 1);
                }
                graphics.DrawString("0:", drawFont, drawBrush, 820, 320);
                graphics.DrawString("-200ms  ", drawFont, drawBrush, 820, 520);
                graphics.DrawString("200ms:", drawFont, drawBrush, 820, 120);

                graphics.DrawString("-200ms:", drawFont, drawBrush, 620, 320);
                graphics.DrawString("200ms:", drawFont, drawBrush, 1020, 320);

                for (int i = -10; i < 11; i++)
                {
                    graphics.DrawLine(p, 820 + 20 * i, 120, 820 + 20 * i, 520);
                    //graphics.DrawString((10 * i).ToString(), drawFont, drawBrush, 10 + 50 * i, 620);

                    graphics.DrawLine(p, 620 , 320 + 20 * i,1020, 320 + 20 * i );
                }


                // graphics.DrawLine(p, 620, 320, 620, 520);
                graphics.DrawLine(p, 620, 320,1020, 320);

                graphics.DrawLine(p, 820, 120,820, 520);

            }
            graphics.Flush();
            graphics.Dispose();
            graphics = null;
            backBitmap.Dispose();
            backBitmap = null;
            wBitmap.AddDirtyRect(new Int32Rect(0,0,width,height));
            wBitmap.Unlock();
        }


        private void DrawECG()
        {

            int width = (int)outCanvasecg.ActualWidth;
            int height = (int)outCanvasecg.ActualHeight;
            if (width > 0 && height > 0)
            {
                DisplayIamgeECG.Width = width;
                DisplayIamgeECG.Height = height;
                wBitmapecg = new WriteableBitmap(width, height, 72, 72, PixelFormats.Bgr24, null);
                DisplayIamgeECG.Source = wBitmapecg;
            }
            wBitmapecg.Lock();
            Bitmap backBitmap = new Bitmap(width, height, wBitmapecg.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, wBitmapecg.BackBuffer);
            Graphics graphics = Graphics.FromImage(backBitmap);
            graphics.Clear(System.Drawing.Color.Gray);

            int leadIndex = btmlead.SelectedIndex;

            double Speed =double.Parse(btmSpedd.Text);

            double AP =  double.Parse(btmApmlity.Text) ;

            //for (int i = 0; i < hrates.Count - 1; i++)
            //{
            //    graphics.DrawLine(Pens.Red, i * 2, 200 - (hrates[i].heartrate - 60) * 4, (i + 1) * 2, 200 - (hrates[i + 1].heartrate - 60) * 4);
            //}
           int OnemmPex = 4;

            //绘制网格 

            int nwide = width / OnemmPex;

            int nhighte= height / OnemmPex;

            //for (int i=0;i<nwide;i++)
            //{

            //    graphics.DrawLine(Pens.Black,   i * OnemmPex , 0 ,  i * OnemmPex ,nhighte* OnemmPex);

            //}
            //for (int i = 0; i < nhighte; i++)
            //{

            //    graphics.DrawLine(Pens.Black, 0, i* OnemmPex, nwide * OnemmPex, i* OnemmPex);

            //}

            double dis = Speed * OnemmPex / 4000;

            int DrawLeng =(int)( width / dis);
            int currentIdnex = (int)slider.Value * 100;
            for (int i=0;i<DrawLeng;i++)
            {
                if (currentIdnex + i > singleDataLength)
                {
                    break;
                }
                if (  currentIdnex+i<singleDataLength)
                {
                    graphics.DrawLine(Pens.Red, (float)(i * dis), (float)(150- ecgData[leadIndex][currentIdnex+i]/10), (float)((i + 1) * dis), (float)(150 - ecgData[leadIndex][currentIdnex + i+1]/10 ));
                }
               
            }

            

            Font drawFont = new Font("Arial", 8);
            SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Yellow);

            graphics.DrawString( btmlead.Text, drawFont, drawBrush, 5, 10);
         

            graphics.Flush();
            graphics.Dispose();
            graphics = null;
            backBitmap.Dispose();
            backBitmap = null;
            wBitmapecg.AddDirtyRect(new Int32Rect(0, 0, width, height));
            wBitmapecg.Unlock();
        }

        //选择数据
        private void btnSelecetPath_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
             
            if (op.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {

                path = op.FileName;

                Dispatcher.BeginInvoke(new Action(() =>
                {

                    tbxpath.Text = path;

                }));
            }
            Init();

        }
        int curentindex = -1;
        private void DisplayIamge_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // curentindex = e.GetPosition.X;
            var pp = e.GetPosition(DisplayIamgeHR);

            curentindex =(int) pp.X;
            Draw();


        }
        int Dstyle = 0;//绘制曲线类型  0   心率趋势图 ，1  直方图,2 频谱图,3 三点图
        private void btmDrawStyle_Click(object sender, RoutedEventArgs e)
        {
            Dstyle++;
            if (Dstyle==0)
            {
                btmDrawStyle.Content = "趋势图";
            }
            if (Dstyle == 1)
            {
                btmDrawStyle.Content = "直方图";
            }
            if (Dstyle == 2)
            {
                btmDrawStyle.Content = "频谱图";
                calHeart.GetPSDRR(listRR);
                DisplayFFTPara();
                //System.Windows.MessageBox.Show(calHeart.m_vecFreqRR.Count.ToString());
            }

            if (Dstyle == 3)
            {
                btmDrawStyle.Content = "散点图";
                
                 
                //System.Windows.MessageBox.Show(calHeart.m_vecFreqRR.Count.ToString());
            }
            if (Dstyle>3)
            {
               
                Dstyle =0;
                btmDrawStyle.Content = "趋势图";
            }
            Draw();
          

        }

        private void btmlead_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
             DrawECG();
        }

        private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
             DrawECG();
        }

        private void btmEcgpath_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();

            if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                string ecgpath = op.FileName;

                Dispatcher.BeginInvoke(new Action(() =>
                {

                    ReadData(ecgpath);
                    DrawECG();

                }));
            }
        }
        CalHeartRate calHeart = new CalHeartRate();
        int pack = 0;
        int nQrspos = 0;

        private void btnCalHr_Click(object sender, RoutedEventArgs e)
        {

            //timer.Start();
            ////获取数据 以下部分属于正常代码
            //int packcount = 1000;
            //double[] dinput = new double[packcount];

            //for (int j = 0; j < packcount; j++)
            //{
            //    dinput[j] = ecgData[1][pack* packcount + j*4];

            //}

            //Thread.Sleep(10);
            //Heartrate = calHeart.GetVCGParameter(dinput, packcount, out nQrspos);

            //Dispatcher.BeginInvoke(new Action(() =>
            //{

            //    txtCalHr.Text = (Heartrate ).ToString() +"-"+ (pack* packcount + nQrspos).ToString();

            //}));

            //pack++;

            ////double[] channel1 = new double[singleDataLength];
            ////for (int i=0;i< singleDataLength;i++)
            ////{
            ////    channel1[i] = ecgData[1][i];
            ////}
            HeartBeatInfoVector hbiv = new HeartBeatInfoVector();//所有心拍信息
           // ECGAlgHelper.FindAllHeartBeats(hbiv, 0, ecgData);
            HNAlgoNative.FindAllFrankHeartBeats(hbiv, (uint)ecgData[0].Length, ecgData[0], ecgData[1], ecgData[2]);

            System.Windows.MessageBox.Show(hbiv.Count.ToString());
        }

        int packcount = 500;
        int preqrspos = 0;
        private void Timer_Tick(object sender, EventArgs e)
        {
            //模拟实时计算心率
          

            double[] dinput = new double[packcount];
            if (pack < singleDataLength / packcount - 1)
            {
                for (int j = 0; j < packcount; j++)
                {
                    //抽点降采样率
                   dinput[j] = ecgData[1][pack * packcount + j * 4];
                   
                    //计算平均值降采样率
                    // dinput[j] = (ecgData[1][pack * packcount + j*4]+ecgData[1][pack * packcount + j * 4 + 1]+ecgData[1][pack * packcount + j * 4 + 2]+ ecgData[1][pack * packcount + j * 4 + 3])/4;
                }

                Thread.Sleep(10);
                Heartrate = calHeart.GetVCGParameter(dinput, packcount, out nQrspos);

                Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (preqrspos < pack * packcount + nQrspos && Heartrate > 0)
                    {
                        //lsithr.Add(txtCalHr.Text);
                        preqrspos = pack * packcount + nQrspos;
                        //dictecg.Add(pack * packcount + nQrspos, Heartrate * 2);
                        txtCalHr.Text =  (Heartrate).ToString() + "-" + (pack * packcount + nQrspos).ToString();
                    }

                   

                }));
            }
            else
            {
                pack = 0;
            }
            pack++;
        }
        private void btncancelCal_Click(object sender, RoutedEventArgs e)
        {
            timer.Stop();
        }
        
        private void calHRVFFT()
        {
            if (calHeart.m_vecFreqRR!=null&& calHeart.m_vecFreqRR.Count>0)
            { 
                //qiu

            }
        
        }



        private void DisplayFFTPara()
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {

                tbxallPD.Text = ((int)calHeart.hRVFFTPara.ALLPSD).ToString();

                tbxVLF.Text = ((int)calHeart.hRVFFTPara.VLF).ToString();

                tbxLF.Text = ((int)calHeart.hRVFFTPara.LF).ToString();

                tbxHF.Text = ((int)calHeart.hRVFFTPara.HF).ToString();

                tbxLFNormal.Text = ((int)calHeart.hRVFFTPara.LFnormal).ToString();

                tbxHFNormal.Text = ((int)calHeart.hRVFFTPara.HFnormal).ToString();

            }));

        }
        private void btnCalFFTRR_Click(object sender, RoutedEventArgs e)
        {
            calHeart.GetPSDRR(listRR);
            Dstyle=2;
            Draw();
            DisplayFFTPara();



        }
    }
}
